<html>
  <head>
    <title>Cache Expiration Demo</title>
    <link rel="stylesheet" href="styles.css">
    <script>
      // If we're running on a real web server (as opposed to localhost, which is whitelisted),
      // then change the protocol to HTTPS.
      // See https://goo.gl/lq4gCo for an explanation as to why this is needed for some features.
      (function() {
        var isLocalhost = !!(window.location.hostname === 'localhost' ||
          // [::1] is the IPv6 localhost address.
          window.location.hostname === '[::1]' ||
          // 127.0.0.1/8 is considered localhost for IPv4.
          window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));
        if (window.location.protocol === 'http:' && !isLocalhost) {
          // Redirect to https: if we're currently using http: and we're not on localhost.
          window.location.protocol = 'https:';
        }
      })();
    </script>
  </head>

  <body>
    <h1>Cache Expiration Demo</h1>

    <h3>Background</h3>
    <p>
      The <a href="service-worker.js" target="_blank">service worker</a> in this example
      demonstrates using the <code>maxCacheEntries</code> and <code>maxCacheAgeSeconds</code>
      options. It uses a dedicated cache to hold YouTube video thumbnails. That
      dedicated cache will purge entries once they're older than 30 seconds, and store at most 10
      entries. It uses the <code>cacheFirst</code> strategy, so any responses that are still in the
      cache will be used directly, without going against the network.
    </p>

    <p>
      While this example uses both <code>maxCacheEntries</code> and <code>maxCacheAgeSeconds</code>,
      it's possible to use each of those options independently.
    </p>

    <p>
      The cache used for YouTube thumbnail URLs is separate from the "default" cache, which is
      used for all other requests, like YouTube API responses and this page's CSS, JavaScript, and
      HTML. The page doesn't impose any upper limit on the size of that default cache, and we can
      use a <code>networkFirst</code> strategy for it.
    </p>

    <p>
      Creating a dedicated cache with expiration options for dynamic, unbounded requests is a useful
      pattern to follow. If we just used the default cache without imposing a cache expiration,
      then that cache would grow in size as more and more searches were performed, needlessly
      consuming disk space for old thumbnails that are likely no longer needed.
    </p>

    <h3>Live Demo</h3>
    <p>
      Try increasing the number of thumbnails returned, or changing the search term, and then
      observe the cache expirations logged in the developer console.
    </p>
    <form id="search">
      <label for="searchTerm">Search YouTube for</label>
      <input id="searchTerm" type="search" value="javascript">
      <label for="maxResults">, returning at most</label>
      <input id="maxResults" type="number" min="1" max="50" value="10">
      <span>thumbnails.</span>
      <input type="submit" value="Search">
    </form>

    <div id="results"></div>

    <script src="app.js"></script>
    <script src="/companion.js" data-service-worker="service-worker.js"></script>
  </body>
</html>
